package com.gonsin.i18n.app;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

import java.util.HashMap;


public class FontTextView extends AppCompatTextView {

    public static int DEFAULT_FONT = 2;
    public static int FONT_HEAVY = 4;
    public static int FONT_SANS_REGULAR = 5;
    public static int FONT_SANS_MEDIUM = 6;

    public static String[] fonts = new String[]{
        "fonts/SourceHanSerifCN-Regular.otf",
        "fonts/SourceHanSerifCN-Medium.otf",
        "fonts/SourceHanSerifCN-SemiBold.otf",
        "fonts/SourceHanSerifCN-Bold.otf",
        "fonts/SourceHanSerifCN-Heavy.otf",
        "fonts/HarmonyOS_Sans_Regular.ttf",
        "fonts/HarmonyOS_Sans_Medium.ttf"
    };

    public static Integer[] allBold = new Integer[]{null, null, null, null, 3, null, null};

    private int defaultFont = DEFAULT_FONT;
    private int defaultSize = 24;

    private Context mContext;
    private static HashMap<Integer, Typeface> typeface = new HashMap<>();


    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }
    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
    public FontTextView(Context context) {
        super(context);
        mContext = context;
        setTypeface(defaultFont);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Text);
        int font = ta.getInt(R.styleable.Text_typeface, defaultFont);
//        int font = defaultFont;
        //float size = ta.getDimension(R.styleable.Text_android_textSize, defaultSize);

        if (ta.getBoolean(R.styleable.Text_bold, false)) {
            getPaint().setFakeBoldText(true);
        }

        ta.recycle();

        setTypeface(font);
    }


    public static Typeface getTypeface(TextView textView, int font) {
       if (font < 0) { error(); font = DEFAULT_FONT; }

        if (allBold.length > font) {
            Integer realBold = allBold[font];
            if (realBold != null) {
                font = realBold;
                textView.getPaint().setFakeBoldText(true);
            } else textView.getPaint().setFakeBoldText(false);
        }
        if (font >= fonts.length) { error(); font = DEFAULT_FONT; }

        return getTypeface(textView.getContext(), font);
    }

    public static Typeface getTypeface(Context context, int font) {
        if (typeface.get(font) == null) {
            typeface.put(font, Typeface.createFromAsset(context.getAssets(), fonts[font]));
        }
        return typeface.get(font);
    }

    public static void setTypeface(TextView textView, int font) {
        textView.setIncludeFontPadding(false);
        textView.setTypeface(getTypeface(textView, font));
    }

    public void setTypeface(int font) {
        setTypeface(this, font);
    }


    private static void error() {
        Log.e(FontTextView.class.getName(), Log.getStackTraceString(new Throwable("无定义字体")));
    }

}
